package org.yunmai.controller;

import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.yunmai.dto.RolePageQuery;
import org.yunmai.entity.SysRole;
import org.yunmai.service.SysRoleService;
import org.yunmai.util.R;
import org.yunmai.vo.PageResult;
import org.yunmai.vo.SysRoleVO;

import java.util.List;

/**
 * @Author 李孝辉
 * @Date 2025/6/3 22:22
 * @email 1626285873@qq.com
 * @Phone 18724505266
 * @内容
 */
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {

    @Resource
    SysRoleService sysRoleService;

    @GetMapping("/showAll")
    public R<List<SysRole>> showAll()
    {
        return R.success(sysRoleService.list());
    }

    @PostMapping("/getById")
    public R<SysRole> getById(Integer id) {
        return R.success(sysRoleService.getById(id));
    }

    @DeleteMapping("/deleteById/{id}")
    public R<Boolean> deleteById(@PathVariable Integer id) {
        return R.success(sysRoleService.removeById(id));
    }

    @PutMapping("/update/{id}")
    public R<Boolean> update(@RequestBody SysRole sysRole) {
        if (sysRole.getId() == null) {
            return R.error("id不能为空");
        }
        boolean result = sysRoleService.updateById(sysRole);
        return R.success(result);
    }
    @PostMapping("/PageList")
    public R<PageResult<SysRoleVO>> getRolePageList(@RequestBody RolePageQuery query) {
        List<SysRoleVO> roleVOS = sysRoleService.getRolePage(query.getPageNum(), query.getPageSize(), query.getKeyword());
        int total = roleVOS.toArray().length;
        PageResult<SysRoleVO> result = new PageResult<>();
        result.setList(roleVOS);
        result.setTotal(total);
        return R.success(result);
    }

    @PostMapping("/add")
    public R<Boolean> add(@RequestBody SysRole sysRole) {
        boolean result = sysRoleService.save(sysRole);
        return R.success(result);
    }
}
